<%@page import="com.charlie.beans.Roles"%>
<%@page import="java.util.List"%>
<%@page import="com.charlie.beans.Groups"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%!
    public int int_val(Object value) {
        if (value == null) {
            return -1;
        }
        return ((Integer) value).intValue();
    }

    public String check_field(Object value) {
        int state = int_val(value);
        String errors = "Field is required";

        return state >= 0 ? errors : "";
    }
%>
<%@include file="/header.jsp"%>

<%
    String id = request.getParameter("groupId");
    Groups g = (Groups) request.getAttribute("editGroup");
%>

<div class="left_side">
    <jsp:include page="/blocks/conditionalLeftBar.jsp" />
</div>

<div class="right_side">
    <h2 class="content_title" style="font-size: x-large;">Create group</h2>
    <div class="white_block"> 
        <h2  style="font-size: large; text-align: center;">Information about group</h2>   <p>
        <form method="post">  
            Group name: <p>
                <input type="text" name="groupName" size="90%" value="<%=g.getName()%>"><br>
                <span class="error_msg"><%=check_field(request.getAttribute("error_field"))%></span><p>
                User's role: <p>
            <table class="typycal_table">
                <tr>
                    <%
                        List<Roles> roles = (List<Roles>) request.getAttribute("rolesList");
                        List<Roles> ownRoles = (List<Roles>) request.getAttribute("groupRole");
                    %>
                <span class="checkRole">            
                    <%
                        if (roles != null)
                            for (Roles r : roles) {
                                if (!r.getRolesName().equals("Admin")) {
                                    if (ownRoles.contains(r.getRolesName())) {
                    %>
                    <td>
                        <input type="checkbox" checked="checked" name="id" value="<%=r.getRolesName()%>"><%=r.getRolesName()%></label>
                    </td>
                    <%} else {%>
                    <td>
                        <input type="checkbox" name="id" value="<%=r.getRolesName()%>"><%=r.getRolesName()%></label>
                    </td>
                    <%}
                            }
                        }%>
                </span>
                <td><input type="submit" size="40" value="Edit"></td>
            </table>
        </form>
        <table>
            <td>
                <form method="post" action="addUsers?groupId=<%=g.getGroupsId()%>">
                    <input type="submit" size="40" value="Add user">
                </form>
            </td>
            <td>
                <form method="post" action="groupDeleteUser?groupId=<%=g.getGroupsId()%>">
                    <input type="submit" size="40" value="Delete user">
                </form>
            </td>
        </table>
    </div>
</div>